<template>
	<view class="content">
		<u-navbar title="地址管理" :placeholder='true' height="88rpx" leftIconColor="#fff"
			titleStyle='color:#fff;font-size:32rpx' left-icon-size="32rpx" bg-color="#FF594D" :autoBack="true">
		</u-navbar>
		<top-line></top-line>
		<view class="address_content">
			<u-radio-group v-model="radiovalue1" placement="column" @change="groupChange">
				<template v-for="(item,index) in bookList">
					<view class="address_item" :key="index" @click="selectAddress(item.id)">
						<view class="address_bar">
							{{item.provinceName+item.cityName+item.districtName+item.detail}}
						</view>
						<view class="user_info">
							<text>{{item.consignee}}</text>{{item.phone}}
						</view>
						<view class="line">
							<u-line></u-line>
						</view>
						<view class="default_address">
							<view class="check_address">
								<u-radio activeColor="#ff594d" size="37rpx" :name="item.id"></u-radio>
								默认地址
							</view>
							<view class="address_operation">
								<view class="operation_item"
									@click.stop="navigateTo('/pages_mine/pages/add-address/add-address?id='+item.id)">
									<image src="../../static/images/address-administration/Frame@2x.png" mode=""></image>
									<text>编辑</text>
								</view>
								<view @click.stop="delAddress(item)" class="operation_item">
									<image src="../../static/images/address-administration/Frame@2x (1).png" mode=""></image>
									<text>删除</text>
								</view>
							</view>
						</view>
					</view>
				</template>
			</u-radio-group>
		</view>
		<u-popup :show="delModel_show" round="10" mode="center">
			<view class="delModel">
				<view class="delTitle">
					<text>删除地址</text>
					<view class="" @click="delModel_show = false">
						<image src="../../static/images/address-administration/图标@2x.png" mode=""></image>
					</view>
				</view>
				<view class="delText">
					是否删除该地址
				</view>
				<view class="operation">
					<view @click="delModel_show = false" class="btn operation_left">
						取消
					</view>
					<view @click="del" class="btn operation_right">
						删除
					</view>
				</view>
			</view>
		</u-popup>
		<view class="add_address" @click="navigateTo('/pages_mine/pages/add-address/add-address')">
			新增地址
		</view>
	</view>
</template>

<script>
	import {
		getBookl,
		delBook,
		updataBook,
	} from '../../../api/book.js'
	export default {
		onLoad(option) {
			this.select = option.select
		},
		onShow() {
			this.getBooklData({
				userId: this.$store.getters.getUserInfo.id
			})
		},
		data() {
			return {
				titleStyle: {
					color: '#fff',
				},
				radiovalue1: '苹果',
				delModel_show: false,
				bookList: [],
				add: {},
				select: 0
			}
		},
		created() {
			this.getBooklData({
				userId: this.$store.getters.getUserInfo.id
			})
		},
		methods: {
			selectAddress(id) {
				if (this.select == 1) {
					// console.log(id);
					this.$store.commit('setAddressId', id)
					uni.navigateBack()
				}
			},
			// 修改默认地址
			async updataBookData(data) {
				const res = await updataBook(data)
				// console.log(res);
			},
			// 获取地址列表
			async getBooklData(data) {
				const res = await getBookl(data)
				console.log(res);
				this.bookList = res.rows
				res.rows.forEach(item => {
					if (item.isDefault == 1) {
						this.radiovalue1 = item.id
					}
				})
			},
			groupChange(name) {
				// console.log(name);
				this.updataBookData({
					id: name,
					isDefault: 1
				})
			},
			radioChange(name) {
				console.log(name);
			},
			navigateTo(url) {
				uni.navigateTo({
					url
				})
			},
			// 删除地址
			delAddress(item) {
				this.add = item
				this.delModel_show = true
			},
			async del() {
				console.log(this.add);
				const ids = this.add.id
				const res = await delBook(ids)
				// console.log(res);
				this.getBooklData()
				this.delModel_show = false
			}
		}
	}
</script>

<style lang="scss">
	.content {
		background-color: #f3f3f3;
		min-height: 100vh;
		padding-bottom: 150rpx;

		.u-safe-area-inset-bottom {
			padding: 0;
		}

		.address_content {
			padding: 30rpx 20rpx;

			.address_item {
				width: 710rpx;
				background: #FFFFFF;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				box-sizing: border-box;
				padding: 24rpx 20rpx;
				margin-bottom: 20rpx;

				.address_bar {
					font-size: 32rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 800;
					color: #333332;
					margin-bottom: 10rpx;
				}

				.user_info {
					font-size: 32rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					color: #777777;

					text {
						padding-right: 30rpx;
					}
				}

				.line {
					padding: 30rpx 0;
				}

				.default_address {
					display: flex;
					justify-content: space-between;

					.check_address {
						position: relative;
						display: flex;
						align-items: center;
						font-size: 26rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						color: #777777;

					}

					.address_operation {
						display: flex;
						font-size: 24rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						color: #777777;
						align-items: center;

						image {
							width: 32rpx;
							height: 32rpx;
							margin-right: 6rpx;
						}

						.operation_item {
							display: flex;
							align-items: center;
							margin-left: 16rpx;
						}
					}
				}
			}
		}

		.delModel {
			width: 656rpx;
			height: 339rpx;
			box-sizing: border-box;
			padding: 24rpx 20rpx;

			.delTitle {
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #333332;
				display: flex;
				justify-content: space-between;

				image {
					width: 32rpx;
					height: 32rpx;
				}
			}

			.delText {
				height: 170rpx;
				text-align: center;
				line-height: 170rpx;
			}

			.operation {
				display: flex;
				justify-content: center;

				.btn {
					width: 170rpx;
					height: 56rpx;
					line-height: 56rpx;
					text-align: center;
					box-sizing: border-box;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
				}

				.operation_left {
					border: 1rpx solid #FF594D;
					color: #FF594D;
					margin-right: 37rpx;
				}

				.operation_right {
					background: #FF594D;
					color: #FFFFFF;
				}
			}
		}

		.add_address {
			width: 600rpx;
			height: 68rpx;
			background: #FF594D;
			border-radius: 74rpx 74rpx 74rpx 74rpx;
			font-size: 24rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			line-height: 68rpx;
			color: #FFFFFF;
			text-align: center;
			position: fixed;
			bottom: 90rpx;
			left: 75rpx;
		}
	}
</style>